package com.yl.applicationsecurity.utils;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description: 节假日计算类
 * @date 2025-04-27 14:39:45
 */
public class StrictHolidayCalculatorUtil {
    // 休息日（节假日+调休放假）
    private static final Set<LocalDate> holidays = new HashSet<>();

    // 补班日（虽然是周末，但要上班）
    private static final Set<LocalDate> workdays = new HashSet<>();

    static {
        // 元旦
        holidays.add(LocalDate.of(2025, 1, 1));

// 春节
        holidays.add(LocalDate.of(2025, 1, 29));
        holidays.add(LocalDate.of(2025, 1, 30));
        holidays.add(LocalDate.of(2025, 1, 31));
        holidays.add(LocalDate.of(2025, 2, 1));
        holidays.add(LocalDate.of(2025, 2, 2));
        holidays.add(LocalDate.of(2025, 2, 3));
        holidays.add(LocalDate.of(2025, 2, 4));

// 清明节
        holidays.add(LocalDate.of(2025, 4, 5));
        holidays.add(LocalDate.of(2025, 4, 6));
        holidays.add(LocalDate.of(2025, 4, 7));

// 劳动节
        holidays.add(LocalDate.of(2025, 5, 1));
        holidays.add(LocalDate.of(2025, 5, 2));
        holidays.add(LocalDate.of(2025, 5, 3));

// 端午节
        holidays.add(LocalDate.of(2025, 5, 31));
        holidays.add(LocalDate.of(2025, 6, 1));
        holidays.add(LocalDate.of(2025, 6, 2));

// 中秋节
        holidays.add(LocalDate.of(2025, 10, 6));

// 国庆节
        holidays.add(LocalDate.of(2025, 10, 1));
        holidays.add(LocalDate.of(2025, 10, 2));
        holidays.add(LocalDate.of(2025, 10, 3));
        holidays.add(LocalDate.of(2025, 10, 4));
        holidays.add(LocalDate.of(2025, 10, 5));
        holidays.add(LocalDate.of(2025, 10, 6));  // 中秋节重叠国庆
        holidays.add(LocalDate.of(2025, 10, 7));

    }

    public static void main(String[] args) {

        int A = 7;
        String d = "2025-05-06 07:29:40";
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(d, formatter2);
        LocalDateTime target = calculateTargetDate(dateTime, A);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M月d日HH:mm");
        System.out.println("结果是：" + target.format(formatter));
    }

    public static LocalDateTime calculateTargetDate(LocalDateTime start, int daysToAdd) {
        LocalDateTime date = start;
        int addedDays = 0;

        while (addedDays < daysToAdd) {
            date = date.plusDays(1);
            if (isWorkday(date.toLocalDate())) {
                addedDays++;
            }
        }

        return date;
    }

    private static boolean isWorkday(LocalDate date) {
        // 优先判断补班日
        if (workdays.contains(date)) {
            return true;
        }
        // 是节假日就是休息日
        if (holidays.contains(date)) {
            return false;
        }
        // 周六周日默认休息
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        return !(dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY);
    }
}

